package com.bit101.components {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;

	/**
	 *  Panel框
	 * 
	 */	
	public class Panel extends Component {
		protected var _mask:Sprite;
		protected var _background:Sprite;
		protected var _bgcolor:int = -1;
		protected var _shadow:Boolean = true;
		protected var _gridSize:int = 10;
		protected var _showGrid:Boolean = false;
		protected var _gridColor:uint = 0xd0d0d0;
		//显示内容的对象
		public var content:Sprite;

		public function Panel(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0){
			super(parent, xpos, ypos);
		}

		/**
		 * Initializes the component.
		 */
		override protected function init():void {
			super.init();
			setSize(100, 100);
		}

		/**
		 * 创建添加子显示对象.
		 * _background
		 * _mask
		 * _content
		 */
		override protected function addChildren():void {
			_background = new Sprite();
			super.addChild(_background);  
			_mask = new Sprite();    //遮罩层
			_mask.mouseEnabled = false;
			super.addChild(_mask);
			content = new Sprite(); //被遮罩层
			super.addChild(content);
			content.mask = _mask;
			filters = [getShadow(2, true)];
		}

		/**
		 * 重写DisplayObjectContainer添加子对象方法
		 * 向被遮罩层添加对象
		 * 返回子对象
		 */
		public override function addChild(child:DisplayObject):DisplayObject {
			content.addChild(child);
			return child;
		}

		/**
		 * 不在被遮罩层中添加对象
		 * super.addChild(child);
		 */
		public function addRawChild(child:DisplayObject):DisplayObject {
			super.addChild(child);
			return child;
		}

		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void {
			super.draw();
			//绘制背景层
			_background.graphics.clear();
			_background.graphics.lineStyle(1, 0, 0.4);
			if (_bgcolor == -1){
				_background.graphics.beginFill(Style.PANEL);
			} else {
				_background.graphics.beginFill(_bgcolor);
			}
			_background.graphics.drawRect(0, 0, _width, _height);
			_background.graphics.endFill();
			drawGrid();
			
			//遮罩层
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xff0000);
			_mask.graphics.drawRect(0, 0, _width, _height);
			_mask.graphics.endFill();
		}

		/**
		 * 绘制格子背景 
		 * 宽_width
		 * 高_height
		 * 默认间隔_gridSize = 10
		 */		
		protected function drawGrid():void {
			if (!_showGrid)
				return;
			_background.graphics.lineStyle(0, _gridColor);
			for (var i:int = 0; i < _width; i += _gridSize){
				_background.graphics.moveTo(i, 0);
				_background.graphics.lineTo(i, _height);
			}
			for (i = 0; i < _height; i += _gridSize){
				_background.graphics.moveTo(0, i);
				_background.graphics.lineTo(_width, i);
			}
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		/**
		 * 设置滤镜效果
		 */
		public function set shadow(b:Boolean):void {
			_shadow = b;
			if (_shadow){
				filters = [getShadow(2, true)];
			} else {
				filters = [];
			}
		}

		public function get shadow():Boolean {
			return _shadow;
		}

		/**
		 * 重设背景颜色，重绘背景
		 */
		public function set color(c:int):void {
			_bgcolor = c;
			invalidate();
		}

		public function get color():int {
			return _bgcolor;
		}

		/**
		 * 设置间隔大小，重绘背景
		 */
		public function set gridSize(value:int):void {
			_gridSize = value;
			invalidate();
		}

		public function get gridSize():int {
			return _gridSize;
		}

		public function set showGrid(value:Boolean):void {
			_showGrid = value;
			invalidate();
		}

		public function get showGrid():Boolean {
			return _showGrid;
		}

		/**
		 * 设置格子线条颜色
		 * @param value
		 */		
		public function set gridColor(value:uint):void {
			_gridColor = value;
			invalidate();
		}

		public function get gridColor():uint {
			return _gridColor;
		}
	}
}